﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.PostModel;
using WebApi.Models;
using WebApi.Class;
using Yd.Domain.DAL;
using Yd.CommCls;
using Yd.BLL;
using System.Data;
using System.Data.SqlClient;
using Yd.Model;
using System.Text;
namespace WebApi.Controllers
{
    public class KazuoController : ApiController
    {
        public CommDAL commdal = new CommDAL();
        public object InsertKazuo([FromBody]PostSubmitKazuo post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopid < 1 || StringUtils.getIsEmpty(post.kzName) || post.FixPice==0||post.maxPeopleNum==0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            int _fresult = -1;
            yd_kazuo tzorder = new yd_kazuo();
            tzorder.shopid = post.shopid;
            tzorder.kzName = post.kzName;
            tzorder.maxPeopleNum = post.maxPeopleNum;
            tzorder.FixPice = post.FixPice.ToString().StrTodecimal();
            tzorder.kazuoDetails = post.kazuoDetails;
            tzorder.kazuoNum = post.kazuoNum;
            tzorder.kzType = post.kzType;
            _fresult = commdal.InsertKazuo(tzorder);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "添加成功";
                return mbase;
            }
            mbase.code = -1;
            mbase.message = "操作失败";
            return mbase;
        }

        // Post 獲取用戶信息 api/users/PostEditUser
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object EditKazuoInfo([FromBody]PostEditKazuo post)
        {
            ModelBase mbase = new ModelBase();
            if (post.kzId == 0 || (StringUtils.getIsEmpty(post.kazuoName) && post.maxPeopleNum == 0 && post.FixPice == 0 && StringUtils.getIsEmpty(post.kazuoDetails)))
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
           
            StringBuilder sql = new StringBuilder();
            sql.Append("update yd_kazuo set ");
            if (!StringUtils.getIsEmpty(post.kazuoName))
            {
                sql.Append(" kazuoName='" + post.kazuoName + "',");
            }
            if (post.maxPeopleNum > 0)
            {
                sql.Append(" maxPeopleNum=" + post.maxPeopleNum+",");
            }
            if (post.FixPice > 0)
            {
                sql.Append(" FixPice=" + post.FixPice + ",");
            }
            if (!StringUtils.getIsEmpty(post.kazuoDetails))
            {
                sql.Append(" kazuoDetails='" + post.kazuoDetails + "',");
            }
            if (!StringUtils.getIsEmpty(post.kazuoNum))
            {
                sql.Append(" kazuoNum=" + post.kazuoNum + ",");
            }
            sql.Append(" updateTime=GETDATE() where kzid=" + post.kzId);
            int _fresult = -1;
            _fresult = commdal.SaveOrUpdateSql(sql.ToString());

            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "修改成功";
                return mbase;
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }
        /// Post 獲取卡座列表 api/kazuo/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object kazuoList([FromBody]PostKazuoList post)
        {
            ModelBase mbase = new ModelBase();
            if (post.shopId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            string pagewhere = " shopid="+post.shopId;
            if (post.isShop != 1) { 
                //用户端请求的时候，不返回不可预定的卡座
                pagewhere += " and isnull(kazuoNum,0)>0 ";
                if (!StringUtils.getIsEmpty(post.kzType)) {
                    pagewhere += " and kzType="+post.kzType;
                }
            }
            if (post.pageSize == 0)
            {
                post.pageSize = 10;
            }
            DataSet newlist = commdal.QueryObjectAllSql("declare @t int Execute Proce_Sql_PageChange 'view_kazuo','  kzId,kzName,maxPeopleNum,FixPice,nowStatus,isCanReserve, createTime,kazuoDetails,kazuoNum,kzType ',' updateTime desc','" + pagewhere + "'," + post.pageSize + "," + post.pageIndex + ",@t output");
            if (newlist != null)
            {
                mbase.code = 0;
                mbase.data = newlist.Tables[0];
                mbase.message = "获取成功";
            }
            else
            {
                mbase.code = -1;
                mbase.message = ErrorCodeUtil.getCodeMessage(-1);
            }
            return mbase;
        }

        // Post 獲取用戶信息 api/users/PostUserInfo
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object PostKazuoInfo([FromBody]PostKazuoInfoById post)
        {
            ModelBase mbase = new ModelBase();
            if (post.kzId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }
            DataSet dsuser = commdal.QueryObjectAllSql("select * from view_kazuo where kzId="+post.kzId);
            if (dsuser != null && dsuser.Tables[0].Rows.Count > 0)
            {
                KazuoModel userInfo = KazuoModel.GetKazuomodelInfo(dsuser.Tables[0].Rows[0]);
                if (userInfo != null)
                {
                    mbase.code = 0;
                    mbase.data = userInfo;
                    mbase.message = ErrorCodeUtil.getCodeMessage(0);
                }
                else
                {
                    mbase.code = -1;
                    mbase.message = ErrorCodeUtil.getCodeMessage(-1);
                }
            }
            else
            {
                mbase.code = -3;
                mbase.message = ErrorCodeUtil.getCodeMessage(-3);
            }
            return mbase;
        }

        /// Post 改变卡座预定情况 api/kazuo/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object UpdateKazuoReserveStatus([FromBody]PostUpdateKZStatus post)
        {
            ModelBase mbase = new ModelBase();
            if (post.kzId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@kzid",post.kzId),
                new SqlParameter("@isCanReserve",post.isCanReserve)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("UpdateKazuoReserveStatus", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "修改成功";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }

        /// Post 改变卡座预定情况 api/kazuo/shopList
        /// <returns>成功返回0  -1异常 -4 参数错误</returns>
        public object OverKazuoConsumption([FromBody]PostOverKZxiaofei post)
        {
            ModelBase mbase = new ModelBase();
            if (post.kzId == 0)
            {
                mbase.code = -4;
                mbase.message = "参数错误";
                return mbase;
            }

            SqlParameter[] fparam = new SqlParameter[] { 
                new SqlParameter("@kzid",post.kzId)
            };
            int _fresult = -1;
            _fresult = commdal.UpdateDataByPro("UpdateKazuoConsumptionToOver", fparam);
            if (_fresult == 0)
            {
                mbase.code = 0;
                mbase.message = "本次消费已结束";
                return mbase;
            }
            else
            {
                mbase.code = _fresult;
                mbase.message = ErrorCodeUtil.getCodeMessage(_fresult);
            }
            return mbase;
        }
    }
}
